package club.banyuan.reserve.service;

import club.banyuan.reserve.consts.AppConst;
import club.banyuan.reserve.dao.AdminOrdersDao;
import club.banyuan.reserve.mapper.OrdersMapper;
import club.banyuan.reserve.model.Orders;
import club.banyuan.reserve.model.OrdersExample;
import club.banyuan.reserve.vo.AdminOrderListVo;
import club.banyuan.reserve.vo.OrderVo;
import club.banyuan.reserve.vo.PositionVo;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HanChao
 * 描述信息：
 */
@Service
@Transactional(isolation = Isolation.READ_COMMITTED)
public class OrderServiceImpl implements OrderService{

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private AdminOrdersDao adminOrdersDao;

    @Override
    public List<OrderVo> showOrderListByUserId(Integer userId) {

        // 根据用户id查订单历史
        OrdersExample ordersExample = new OrdersExample();
        ordersExample.createCriteria()
                .andUseridEqualTo(userId);
        ordersExample.setOrderByClause("id DESC");
        List<Orders> ordersList = ordersMapper.selectByExample(ordersExample);

        List<OrderVo> list = new ArrayList<>();

        for (Orders o: ordersList) {

            OrderVo order = new OrderVo();
            PositionVo position = new PositionVo();
            order.setId(o.getId());
            order.setCancelFlag(o.getCancelflag());
            order.setCreatedAt(DateUtil.format(o.getCreatedAt(), AppConst.FORMAT_YMD_HM));
            order.setDate(o.getDate());
            order.setHour(o.getHour());

            position.setId(o.getPositionid());
            position.setName(o.getPositionid() + "号座位");

            order.setPosition(position);
            order.setUpdatedAt(DateUtil.format(o.getUpdatedAt(),AppConst.FORMAT_YMD_HM));
            order.setPositionId(o.getPositionid());
            order.setUserId(userId);

            list.add(order);
        }

        return list;
    }

    @Override
    public List<AdminOrderListVo> showAdminOrderList() {

        List<Orders> ordersList = ordersMapper.selectByExample(new OrdersExample());

        List<AdminOrderListVo> list = new ArrayList<>();

        for (Orders o: ordersList) {

            Integer userId = o.getUserid();
            Integer positionId = o.getPositionid();

            AdminOrderListVo order = new AdminOrderListVo();
            order.setId(o.getId());
            order.setCancelFlag(o.getCancelflag());
            order.setCreatedAt(DateUtil.format(o.getCreatedAt(), AppConst.FORMAT_YMD_HM));
            order.setDate(o.getDate());
            order.setHour(o.getHour());
            order.setPositionName(adminOrdersDao.findPositionNameByPositionid(positionId));
            order.setUpdatedAt(DateUtil.format(o.getUpdatedAt(),AppConst.FORMAT_YMD_HM));
            order.setUsername(adminOrdersDao.findUsernameByUserId(userId));

            list.add(order);
        }

        return list;
    }
}
